package ${package};

import ${packageBase}.core.builder.Page;
import ${packageBase}.core.jooq.tables.pojos.${className};
import ${packageBase}.core.model.PageInfo;
import ${packageBase}.core.vo.PageVO;
import ${packageBase}.core.builder.WhereCondition;
import ${packageBase}.${module}.biz.${className}Biz;
import ${packageBase}.${module}.dto.${className}DTO;
import ${packageBase}.${module}.service.${className}Service;
import ${packageBase}.${module}.vo.${className}VO;
import ${packageBase}.${module}.vo.${className}SearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.jooq.impl.DSL;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static ${packageBase}.core.jooq.tables.${className}.${tableName?upper_case};

/**
 * ${tableComment} 业务接口实现层
 *
 * @author motorfu
 * @email ffu@maxleap.com
 * @since ${date}
 */
@Component
public class ${className}BizImpl implements ${className}Biz {

  private final ${className}Service ${classNameSmall}Service;

  @Autowired
  public ${className}BizImpl(${className}Service ${classNameSmall}Service) {
    this.${classNameSmall}Service = ${classNameSmall}Service;
  }

  @Override
  public ${className}DTO create(${className}VO vo) {
    ${className} ${classNameSmall} = ${classNameSmall}Service.create(vo.toEntity());
    return ${className}DTO.toDTO(${classNameSmall});
  }

  @Override
  public ${className}DTO modify(${className}VO vo) {
    ${className} ${classNameSmall} = ${classNameSmall}Service.modify(vo.toEntity());
    return ${className}DTO.toDTO(${classNameSmall});
  }

  @Override
  public int remove(Long id) {
    return ${classNameSmall}Service.remove(id);
  }

  @Override
  public ${className}DTO find(Long id) {
    ${className} ${classNameSmall} = ${classNameSmall}Service.find(id);
    return ${className}DTO.toDTO(${classNameSmall});
  }

  @Override
  public List<${className}DTO> findList() {
  List<${className}> list = ${classNameSmall}Service.findList(null);
    return list.stream()
       .map(${classNameSmall} -> ${className}DTO.toDTO(${classNameSmall}))
       .collect(Collectors.toList());
  }

  @Override
  public PageInfo<${className}DTO> findPage(PageVO<?> pageVO) {
   Page page = Page.newBuilder(pageVO).build();
   //todo params 是否存在 具体分析

   PageInfo<${className}> pageInfo = ${classNameSmall}Service.findPage(page);

   return pageInfo.toDTO(${className}DTO.class);
  }

  @Override
  public PageInfo<${className}DTO> search(${className}SearchVO searchVO) {
    WhereCondition.Builder whereConditionBuilder = WhereCondition.newBuilder(DSL.condition("1=1"));

    //必传参数条件
    ${className}SearchVO.${className}Params params = searchVO.getParams();
    if (params != null) {
      //whereConditionBuilder.andEq(${tableName?upper_case}.CREATED_BY, params.getCreatedBy());
    }
    //搜索条件
    ${className}SearchVO.${className}SearchParams searchParams = searchVO.getSearchParams();
    if (searchParams != null) {
      String all = searchParams.getAll();
      if (!StringUtils.isEmpty(all)) {
        whereConditionBuilder.and(WhereCondition.newBuilder()
          <#list columnList as column>
            <#if column.column_className == 'String'>
            .orLikeBoth(${tableName?upper_case}.${column.column_name2?upper_case}, all)
            </#if>
          </#list>
            .build()
            .getCondition()
        );
      } else {
        //指定搜索参数
        whereConditionBuilder.andEq(${tableName?upper_case}.ID, searchParams.getId());
      <#list columnList as column>
        <#if column.column_className == 'String'>
        whereConditionBuilder.andLikeBoth(${tableName?upper_case}.${column.column_name2?upper_case}, searchParams.get${column.column_name3}());
        </#if>
      </#list>
      }
    }
    Page.Builder pageBuilder = Page.newBuilder(searchVO, whereConditionBuilder.build());
    PageInfo<${className}> pageInfo = ${classNameSmall}Service.findPage(pageBuilder.build());
    return pageInfo.toDTO(${className}DTO.class);
  }


}
